Conditions | 4 |
Total Lines | 51 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import axios from "axios"; |
||
5 | |||
6 | // https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html |
||
7 | |||
8 | export default async function handler( |
||
9 | req: VercelRequest, |
||
10 | res: VercelResponse, |
||
11 | ): Promise<void> { |
||
12 | const { regNummer = "" } = req.query; |
||
13 | |||
14 | if (!regNummer) { |
||
15 | res.status(400).json({ error: "Mangler regNummer parameter" }); |
||
16 | return; |
||
17 | } |
||
18 | |||
19 | const urlToFetch = `https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/felles/datautlevering/enkeltoppslag/kjoretoydata?kjennemerke=${regNummer}`; |
||
20 | |||
21 | try { |
||
22 | const response = await axios.get<IStatensVegvesenFullData>(urlToFetch, { |
||
23 | headers: { |
||
24 | "SVV-Authorization": `Apikey ${process.env.SVV_API_KEY}`, |
||
25 | "X-Client-Identifier": "my-app", |
||
26 | }, |
||
27 | }); |
||
28 | |||
29 | if (response.status === 200) { |
||
30 | const { |
||
31 | kjoretoydataListe: [ |
||
32 | { |
||
33 | kjoretoyId: { kjennemerke }, |
||
34 | forstegangsregistrering: { |
||
35 | registrertForstegangNorgeDato: forstegangsregistrering, |
||
36 | }, |
||
37 | periodiskKjoretoyKontroll: { sistGodkjent: sistKontrollert }, |
||
38 | }, |
||
39 | ], |
||
40 | } = response.data; |
||
41 | |||
42 | const sanitizedData = { |
||
43 | kjennemerke: sanitize(kjennemerke), |
||
44 | forstegangsregistrering: sanitize(forstegangsregistrering), |
||
45 | sistKontrollert: sanitize(sistKontrollert), |
||
46 | }; |
||
47 | |||
48 | res.status(200).json(sanitizedData); |
||
49 | } else { |
||
50 | res |
||
51 | .status(500) |
||
52 | .json({ error: `Feil under henting av data - ${response}` }); |
||
53 | } |
||
54 | } catch (error) { |
||
55 | res.status(500).json({ error: `Feil under henting av data - ${error}` }); |
||
56 | } |
||
58 |